﻿namespace MutexTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            Mutex mutex = new Mutex(false, "Global\\MyAppMutex");

            if (!mutex.WaitOne(0))
            {
                Console.WriteLine("进程锁已被占用，程序退出！");
                try
                {
                    mutex.ReleaseMutex();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            else
            {
                Console.WriteLine("进程锁未被占用，程序继续执行！");
                Console.ReadLine();
                //mutex.ReleaseMutex();//释放锁
            }
            Console.ReadLine();
        }
    }
}
